package xuegao.study.mall.web.service.impl;

import cn.hutool.core.collection.CollUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import xuegao.study.mall.web.domain.CartPromotionItem;
import xuegao.study.mall.web.service.OmsCarItemService;
import xuegao.study.mall.web.service.OmsPromotionService;
import xuegao.study.mall.web.service.UmsMemberService;
import xuegao.study.mall.repo.dao.oms.OmsCarItemDao;
import xuegao.study.mall.common.model.oms.OmsCartItem;
import xuegao.study.mall.common.model.ums.UmsMember;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class OmsCarItemServiceImpl implements OmsCarItemService {

    @Autowired
    private UmsMemberService umsMemberService;

    @Autowired
    private OmsCarItemDao omsCarItemDao;

    @Autowired
    private OmsPromotionService omsPromotionService;

    @Override
    @Transactional
    public int add(OmsCartItem cartItem) {
        int count;
        UmsMember currentMember = umsMemberService.getCurrentMember();
        cartItem.setMemberId(currentMember.getId());
        cartItem.setMemberNickname(currentMember.getNickname());
        cartItem.setDeleteStatus(0);
        OmsCartItem existsOmsCarItem = getCarItem(cartItem);
        if (existsOmsCarItem == null) {
            cartItem.setCreateDate(new Date());
            count = omsCarItemDao.insert(cartItem);
        } else {
            existsOmsCarItem.setModifyDate(new Date());
            existsOmsCarItem.setQuantity(existsOmsCarItem.getQuantity() + cartItem.getQuantity());
            count = omsCarItemDao.updateByPrimaryKey(existsOmsCarItem);
        }
        return count;
    }

    @Override
    public List<OmsCartItem> list(Long memberId) {
        Map<String, Object> params = new HashMap<>();
        params.put("memberId", memberId);
        params.put("deleteStatus", 0);
        return omsCarItemDao.selectByParams(params);
    }

    @Override
    @Transactional
    public List<CartPromotionItem> listPromotion(Long memberId, List<Long> cartIds) {
        // 1.根据memberId和cartIds获取OmsCarItem购物车信息
        List<OmsCartItem> cartItemList = list(memberId);
        if (!CollUtil.isEmpty(cartIds)) {
            cartItemList = cartItemList.stream().filter(item -> cartIds.contains(item.getId())).collect(Collectors.toList());
        }
        // 2.封装成携带促销信息的购物车信息
        List<CartPromotionItem> cartPromotionItemList = new ArrayList<>();
        if (!CollUtil.isEmpty(cartItemList)) {
            cartPromotionItemList = omsPromotionService.calcCartPromotion(cartItemList);
        }
        return cartPromotionItemList;
    }

    @Override
    @Transactional
    public int delete(Long memberId, List<Long> ids) {
        return omsCarItemDao.deleteByParams(memberId, ids);
    }

    /**
     * 根据会员id,商品id和规格获取购物车中的商品
     * @param cartItem
     * @return
     */
    private OmsCartItem getCarItem(OmsCartItem cartItem) {
        Long memberId = cartItem.getMemberId();
        Long productId = cartItem.getProductId();
        Map<String, Object> params = new HashMap<>();
        params.put("memberId", memberId);
        params.put("productId", productId);
        params.put("deleteStatus", 0);
        if (cartItem.getProductSkuId() != null) {
            params.put("productSkuId", cartItem.getProductSkuId());
        }
        List<OmsCartItem> cartItemList = omsCarItemDao.selectByParams(params);
        if (!CollUtil.isEmpty(cartItemList)) {
            return cartItemList.get(0);
        }
        return null;
    }
}
